package com.wsj.web.action.admin.mokuai;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wsj.service.IMokuaiServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.admin.MokuaiForm;
import com.wsj.web.subclass.function.ProductsParamentObj;
import com.wsj.web.vo.view.Mokuai;

public class MokuaiAction extends DispatchAction {

	private IMokuaiServ mokuaiServImpl;

	/** 去添加 */

	public ActionForward toadd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MokuaiForm mokuaiForm = (MokuaiForm) form;

		return mapping.findForward("toadd");
	}

	/** 添加 */

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String mingcheng = request.getParameter("mingcheng");
		String shanchu = request.getParameter("shanchu");
		if ((mingcheng == null || mingcheng.equals(""))
				|| (shanchu == null || shanchu.equals(""))) {
			request.setAttribute("add",
					"<script>alert('添加失败，请认真填写各项！！！');</script>");
			return mapping.findForward("toadd");
		}
		Mokuai mokuai = new Mokuai();
		mokuai.setMingcheng(mingcheng);
		mokuai.setShanchu(Short.valueOf(shanchu));
		mokuai.setQuanxian(new Short("1"));
		mokuai.setMkpx(Integer.valueOf("0"));
		boolean b = this.mokuaiServImpl.add(mokuai);
		if (b) {
			request.setAttribute("add",
					"<script>alert('添加成功，请继续添加！！！');</script>");
			return mapping.findForward("toadd");
		}
		return mapping.findForward("toadd");
	}

	/** 去修改 */

	public ActionForward toupdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		MokuaiForm mokuaiForm = (MokuaiForm) form;
		Mokuai mokuai = null;
		String id = request.getParameter("id");
		String bkshanchu = request.getParameter("bkshanchu");
		String bkmingcheng = request.getParameter("bkmingcheng");

		request.setAttribute("bkmingcheng", bkmingcheng != null ? bkmingcheng
				: "");

		if (id != null && !id.equals("")) {
			mokuai = this.mokuaiServImpl.loadById(Integer.valueOf(id));
		}
		if (mokuai == null) {
			request.setAttribute("update",
					"<script>alert('修改失败，该模块不存在！！！');</script>");
			return new ActionForward("/admin/mokuai.do?method=list");
		}
		BeanUtils.copyProperties(mokuaiForm, mokuai);
		request.setAttribute("mokuaiForm", mokuaiForm);
		return mapping.findForward("toupdate");
	}

	/** 修改 */

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		String mingcheng = request.getParameter("mingcheng");
		String shanchu = request.getParameter("shanchu");
		if ((mingcheng == null || mingcheng.equals(""))
				|| (shanchu == null || shanchu.equals(""))) {
			request
					.setAttribute("update",
							"<script>alert('修改失败！！！');</script>");
			return new ActionForward("/admin/mokuai.do?method=list&shanchu=0");
		}
		Mokuai mokuai = this.mokuaiServImpl.loadById(Integer.valueOf(id));
		mokuai.setMingcheng(mingcheng);
		mokuai.setShanchu(Short.valueOf(shanchu));
		boolean b = this.mokuaiServImpl.update(mokuai);
		if (b) {
			request
					.setAttribute("update",
							"<script>alert('修改成功！！！');</script>");
			StringBuffer buff = new StringBuffer("/admin/mokuai.do?method=list");
			String page = request.getParameter("bkpage");
			if (page != null && !page.equals("")) {
				buff.append("&page=").append(page);
			}
			String pageSize = request.getParameter("bkpageSize");
			if (pageSize != null && !pageSize.equals("")) {
				buff.append("&pageSize=").append(pageSize);
			}
			String bkshanchu = request.getParameter("bkshanchu");
			if (bkshanchu != null && !bkshanchu.equals("")) {
				buff.append("&shanchu=").append(bkshanchu);
			}
			String bkmingcheng = request.getParameter("bkmingcheng");
			if (bkmingcheng != null && !bkmingcheng.equals("")) {
				buff.append("&mingcheng=").append(bkmingcheng);
			}
			return new ActionForward(buff.toString());
		}

		return new ActionForward("/admin/mokuai.do?method=list&shanchu=0");
	}

	/** 查询 */

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		this.loadMokuaiList(request);

		return mapping.findForward("list");
	}

	private void loadMokuaiList(HttpServletRequest request) {

		ProductsParamentObj parament = new ProductsParamentObj();

		String[] ret = parament.dealWithParaments(request, new String[] {
				PageContent.page, PageContent.pageSize, PageContent.doit,
				PageContent.backPage, PageContent.nextPage, "shanchu",
				"mingcheng" });

		int page = PageContent.pageValue;

		int pageSize =15;

		String doit = "";

		// 页码
		if (ret[0] != null) {
			page = Integer.parseInt(ret[0]);
		}

		// 显示数
		if (ret[1] != null) {
			pageSize = Integer.parseInt(ret[1]);
		}

		// 跳转
		if (ret[2] != null) {
			doit = ret[2];
		}

		// 上一页
		if (doit != null && doit.equals(PageContent.backPage) && ret[3] != null
				&& !ret[3].equals("")) {
			page = Integer.parseInt(ret[3]);
		}

		// 下一页
		if (doit != null && doit.equals(PageContent.nextPage) && ret[4] != null
				&& !ret[4].equals("")) {
			page = Integer.parseInt(ret[4]);
		}

		Short shanchu = -2;
		if (ret[5] != null) {
			shanchu = Short.valueOf(ret[5]);
		}
		String mingcheng = "";
		if (ret[6] != null) {
			try {
				mingcheng = new String(ret[6].trim().getBytes("ISO-8859-1"),
						"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Mokuai mokuai = new Mokuai();
		mokuai.setMingcheng(mingcheng);
		mokuai.setShanchu(shanchu);
		mokuai.setPage(page);
		mokuai.setPageSize(pageSize);
		try {

			PageResultSet pageResultSet = this.mokuaiServImpl
					.loadMokuaiByBean(mokuai);
			if (pageResultSet != null) {
				// 设置上一页
				request.setAttribute(PageContent.backPage, pageResultSet
						.getBackPage());
				// 下一页
				request.setAttribute(PageContent.nextPage, pageResultSet
						.getNextPage());
				// 当前页
				request.setAttribute(PageContent.curPage, pageResultSet
						.getCurPage());

				// 总共页数
				request.setAttribute(PageContent.allPage, pageResultSet
						.getAllPage());
				// 大小
				request.setAttribute(PageContent.pageSize, pageResultSet
						.getPageSize());

				request.setAttribute(PageContent.recordNum, pageResultSet
						.getRecordNum());

				request.setAttribute("mingcheng", mingcheng != null ? mingcheng
						: "");
				request.setAttribute("shanchu", shanchu);
				request.setAttribute("page", page);
				// 结果集
				List result = null;
				if (pageResultSet != null) {
					result = pageResultSet.getResult();
				} else {
					result = new ArrayList();
				}

				request.setAttribute("mokuaiList", result);

			}
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}

	/***************************************************************************
	 * 批量处理 商品
	 */
	public ActionForward dealWithAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		// 获取处理操作
		String doit = request.getParameter("doit");
		// 返回参数
		String page = request.getParameter("page");

		// 返回参数
		String pageSize = request.getParameter("pageSize");

		String[] checkboxparam = request.getParameterValues("checkboxparam");

		int[] ids = null;

		if (checkboxparam != null && checkboxparam.length > 0) {
			ids = new int[checkboxparam.length];

			for (int j = 0; j < checkboxparam.length; j++) {
				ids[j] = Integer.valueOf(checkboxparam[j]);
			}
		}

		if (doit == null) {
			doit = "";
		}

		// 放入回收站
		if (doit.equals("isDel")) {
			manyDealWithProducts(ids, "isDel", new Short("1"));
			request.setAttribute("succe", "<script>alert('成功放入回收站');</script>");
		}
		// 恢复
		if (doit.equals("delisDel")) {
			manyDealWithProducts(ids, "isDel", new Short("0"));
			request.setAttribute("succe", "<script>alert('恢复成功');</script>");
		}
		// 彻底删除
		if (doit.equals("readlyisDel")) {
			manyDealWithProducts(ids, "readlyisDel", new Short("1"));
			request.setAttribute("succe", "<script>alert('彻底删除成功');</script>");
		}
		StringBuffer link = new StringBuffer("/admin/mokuai.do?method=list");

		// 页面
		if (page != null && !page.trim().equals("")) {
			link.append("&page=").append(page);
		}
		// 记录数
		if (pageSize != null && !pageSize.trim().equals("")) {
			link.append("&pageSize=").append(pageSize);
		}
		String mingcheng = request.getParameter("bkmingcheng");
		if (mingcheng != null && !mingcheng.equals("")) {

			link.append("&mingcheng=").append(mingcheng);
		}
		String shanchu = request.getParameter("bkshanchu");
		if (shanchu != null && !shanchu.equals("")) {

			link.append("&shanchu=").append(shanchu);
		}

		return new ActionForward(link.toString());
	}

	/***************************************************************************
	 * 处理批量操作商品 比如批量推荐 订单id数组,处理的列,值
	 */
	public void manyDealWithProducts(int[] ids, String column, Object value) {

		try {

			if (column != null && !column.equals("readlyisDel")) {
				if (ids != null && ids.length > 0) {
					for (int j = 0; j < ids.length; j++) {
						Mokuai mokuai = this.mokuaiServImpl.loadById(ids[j]);
						// 放入回收站
						if (column.equals("isDel")) {
							mokuai.setShanchu((Short) value);
						}

						// 更新产品属性
						this.mokuaiServImpl.update(mokuai);

					}
				}
			}

			else {
				if (ids != null && ids.length > 0) {
					for (int j = 0; j < ids.length; j++) {
						this.mokuaiServImpl.delete(ids[j]);
					}
				}

			}

		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}

	public IMokuaiServ getMokuaiServImpl() {
		return mokuaiServImpl;
	}

	public void setMokuaiServImpl(IMokuaiServ mokuaiServImpl) {
		this.mokuaiServImpl = mokuaiServImpl;
	}

}
